<template>
  <div>
    <!-- 顶部大图 -->
    <div class="header_img" @click="toMusicListDetailPage">
      <!-- 模糊背景图片 -->
      <div id="blurry_img"></div>

      <el-image
        style="width: 150px; height: 150px"
        :src="bannerImgUrl"
        fit="fill"
      ></el-image>

      <div class="desc">
        <div class="icon">
          <i class="el-icon-grape"></i>
          精品歌单
        </div>
        <div class="text1">{{ bannerName }}</div>
        <div class="text2">{{ bannerCopywriter }}</div>
      </div>
    </div>

    <!-- 分类栏 -->
    <div class="category_Bar">
      <div class="tags_popover">
        <el-popover
          placement="bottom-start"
          width="860"
          v-model="tagsListVisible"
        >
          <div>
            <div
              class="tags_bar"
              v-for="(theTagCategory, theTagCategoryKey) in allTags"
              :key="theTagCategoryKey"
            >
              <div class="tags_categoryName">
                <i :class="theTagCategory.icon"></i>
                {{ theTagCategory.category }}
              </div>

              <div class="tags_categoryDetail">
                <div
                  v-for="(tag, key) in theTagCategory.list"
                  :key="key"
                  @click="selectTag(tag.id, tag.name)"
                >
                  <span :class="tagActive.tagId == tag.id ? 'tag_active' : ''">
                    {{ tag.name }}
                  </span>
                </div>
              </div>
            </div>
          </div>

          <el-button class="btn" round v-remove-focus slot="reference">
            {{ tagActive.tagName }}
            <i class="el-icon-arrow-right"></i>
          </el-button>
        </el-popover>
      </div>

      <div class="category">
        <span
          v-for="(tag, key) in musicTags"
          :key="key"
          :class="tagActive.tagId == tag.tagId ? 'tag_active' : ''"
          @click="selectTag(tag.tagId, tag.tagName)"
        >
          {{ tag.tagName }}</span
        >
      </div>
    </div>

    <!-- 歌单列表 -->
    <div class="musicList">
      <div
        v-for="item in highQualityList"
        :key="item.id"
        @dblclick="toSonglistPage(item.id)"
      >
        <el-image
          style="width: 160px; height: 160px"
          :src="item.picUrl"
          fit="fill"
        ></el-image>
        <div class="playCount">
          <i class="el-icon-caret-right"></i>
          {{ item.playCount }}
        </div>
        <div class="play">
          <i class="el-icon-caret-right"></i>
        </div>
        <div class="name">{{ item.name }}</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'

export default {
  data() {
    return {
      // 默认音乐标签
      musicTags: [
        { tagName: '华语', tagId: '5001' },
        { tagName: '流行', tagId: '1' },
        { tagName: '摇滚', tagId: '2' },
        { tagName: '民谣', tagId: '1001' },
        { tagName: '电子', tagId: '2004' },
        { tagName: '另类/独立', tagId: '10001' },
        { tagName: '轻音乐', tagId: '2008' },
        { tagName: '影视原声', tagId: '9001' },
        { tagName: 'ACG', tagId: '11002' },
      ],
      // 所有音乐标签分类名称
      allTags: [
        {
          category: '语种',
          icon: 'el-icon-cold-drink',
          list: [],
        },
        {
          category: '风格',
          icon: 'el-icon-cherry',
          list: [],
        },
        {
          category: '场景',
          icon: 'el-icon-watermelon',
          list: [],
        },
        {
          category: '感情',
          icon: 'el-icon-milk-tea',
          list: [],
        },
        {
          category: '主题',
          icon: 'el-icon-potato-strips',
          list: [],
        },
      ],
      // 精品歌单
      highQualityList: [],
      // 当前标签
      tagActive: {
        tagId: '5001',
        tagName: '华语',
      },
      // 顶部大图url
      bannerImgUrl: '',
      // 顶部大图标题
      bannerName: '',
      // 顶部大图文案
      bannerCopywriter: '',
      // 歌单标签列表显示与否
      tagsListVisible: false,
    }
  },

  created() {
    this.getHighQualityTags()
    this.getHighQualityList()
  },
  watch: {
    'tagActive.tagId': function (newVal, oldVal) {
      this.getHighQualityList()
    },
  },
  methods: {
    ...mapMutations(['changePath', 'changeIsActive', 'changeSonglistId']),

    // 获取精品歌单标签列表
    async getHighQualityTags() {
      const { data: res } = await this.$http({
        url: `/playlist/highquality/tags`,
        withCredentials: true, //关键
      })

      // console.log(res)
      // 语种 风格 场景 情感 主题
      let languageList = []
      let styleList = []
      let scenesList = []
      let emotionList = []
      let themeList = []
      res.tags.map((item) => {
        let listObj = {}

        listObj.id = item.id
        listObj.name = item.name
        listObj.category = item.category

        if (item.category == 0) {
          languageList.push(listObj)
        }
        if (item.category == 1) {
          styleList.push(listObj)
        }
        if (item.category == 2) {
          scenesList.push(listObj)
        }
        if (item.category == 3) {
          emotionList.push(listObj)
        }
        if (item.category == 4) {
          themeList.push(listObj)
        }
      })
      this.allTags[0].list = languageList
      this.allTags[1].list = styleList
      this.allTags[2].list = scenesList
      this.allTags[3].list = emotionList
      this.allTags[4].list = themeList

      // console.log(this.allTags)
    },
    // 选择精品歌单
    selectTag(tagId, tagName) {
      this.tagActive.tagId = tagId
      this.tagActive.tagName = tagName
    },
    // 获取精品歌单标签列表
    async getHighQualityList() {
      const { data: res } = await this.$http({
        url: `/top/playlist/highquality?cat='${this.tagActive.tagName}'&limit=20`,
        withCredentials: true, //关键
      })

      // console.log(res)
      let tmepListDatas = []
      res.playlists.map((item) => {
        let listObj = {}

        listObj.id = item.id
        listObj.name = item.name
        listObj.picUrl = item.coverImgUrl
        listObj.playCount = item.playCount
        listObj.copywriter = item.copywriter

        return tmepListDatas.push(listObj)
      })

      this.highQualityList = tmepListDatas
      this.bannerName = this.highQualityList[0].name
      this.bannerImgUrl = this.highQualityList[0].picUrl
      this.bannerCopywriter = this.highQualityList[0].copywriter
      const bg = document.getElementById('blurry_img')
      bg.style.backgroundImage = 'url("' + this.highQualityList[0].picUrl + '")'
    },
    // 跳转到精品歌单详情页面
    toMusicListDetailPage() {
      const info = {
        path: '/view/find/musiclist/musiclistdetail',
        tagActive: {
          tagId: this.tagActive.tagId,
          tagName: this.tagActive.tagName,
        },
      }
      this.$emit('toMusicListDetailPage', info)
    },
    // 传递 歌单数据，并跳转到歌单页面
    toSonglistPage(songlistId) {
      const info = {
        path: '/view/songlist',
        isActive: songlistId,
        songlistId: songlistId,
      }

      this.changePath(info.path)
      this.changeIsActive(info.isActive)
      this.changeSonglistId(info.songlistId)

      this.$emit('getSonglistId')
    },
  },
}
</script>

<style lang="less" scoped>
.header_img {
  width: 900px;
  height: 150px;
  background-color: rgb(245, 200, 200);
  padding: 16px;
  display: flex;
  border-radius: 8px;
  overflow: hidden;
  position: relative;

  > #blurry_img {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 1200px;
    height: 1200px;
    overflow: hidden;
    // background-image: url('http://p1.music.126.net/UDpjFEHmXInxGd_xMAI12w==/109951162811986419.jpg');
    background-repeat: no-repeat;
    background-size: 1200px;
    background-position: center center;
    filter: blur(60px);
  }
  > .el-image {
    padding-right: 16px;
    /deep/ .el-image__inner {
      border-radius: 8px;
    }
  }
  > .desc {
    position: relative;
    top: 20px;
    > .icon {
      border: 2px solid #e2a85a;
      color: #e2a85a;
      width: 85px;
      padding: 6px 16px;
      border-radius: 30px;
    }
    > .text1 {
      padding-top: 20px;
      color: white;
      font-size: 18px;
    }
    > .text2 {
      padding-top: 10px;
      color: #c3bcaf;
      font-size: 14px;
    }
  }
}

// 弹出框样式
.tags_bar {
  display: flex;
  justify-content: space-between;
  .tags_categoryName {
    width: 100px;
    font-size: 16px;
    padding-top: 10px;
    padding-bottom: 5px;
    padding-left: 50px;
    color: #cfcfcf;
    position: relative;
    > i {
      color: #999999;
      font-size: 30px;
      position: absolute;
      left: 13px;
      top: 7px;
    }
  }
  .tags_categoryDetail {
    width: 700px;
    display: flex;
    flex-wrap: wrap;
    padding-top: 10px;
    padding-bottom: 5px;

    > div {
      width: 110px;
      padding-bottom: 15px;
      > span {
        padding: 6px 12px;
        font-size: 16px;
        cursor: pointer;
      }
      > span:hover {
        color: #ec4141;
      }
    }
  }
  .tag_active {
    background-color: #fdf5f5;
    color: #ec4141;
    border-radius: 30px;
  }
}

.category_Bar {
  padding: 16px 0;
  display: flex;
  justify-content: space-between;
  > .tags_popover {
    .btn {
      padding: 7px 24px;
      font-size: 17px;
    }
  }

  > .category {
    position: relative;
    padding-top: 10px;
    > span {
      padding: 0px 6px;
      font-size: 15px;
      color: #373737;
      cursor: pointer;
    }
    > span.tag_active {
      background-color: #fdf5f5;
      color: #ec4141;
      padding: 4px 8px;
      border-radius: 30px;
    }
  }
}

.musicList {
  display: flex;
  flex-wrap: wrap;
  > div {
    padding: 0px 12px 20px 12px;
    position: relative;
    cursor: pointer;
    .el-image {
      border-radius: 6px;
    }
    > .playCount {
      position: absolute;
      right: 17px;
      top: 2px;
      color: white;
      font-size: 13.6px;
    }
    > .play {
      position: absolute;
      right: 22px;
      bottom: 76px;
      border-radius: 50%;
      width: 32px;
      height: 31px;
      background-color: white;
      transition: 1s;
      opacity: 0;
      > .el-icon-caret-right {
        font-size: 28px;
        color: #ec4141;
        position: relative;
        left: 3px;
        top: 1px;
      }
    }
    > .name {
      width: 160px;
      font-size: 15px;
      padding-top: 4px;
      overflow: hidden;
      text-overflow: ellipsis;
      //将对象作为弹性伸缩盒子模型显示。
      display: -webkit-box;
      //从上到下垂直排列子元素（设置伸缩盒子的子元素排列方式）
      -webkit-box-orient: vertical;
      //这个属性不是css的规范属性，需要组合上面两个属性，表示显示的行数。
      -webkit-line-clamp: 2;
    }
  }

  > div:hover {
    transition: 1s;
    > .play {
      opacity: 1;
    }
  }
}
</style>